import {defineStore} from "pinia";
import {Pressure} from "../types/tanTypes/pressure";
import {reactive} from "vue";
import {getPressureT} from "../api/eventList/event";

// 定义时间地点查询数据包接口
interface AddressState {
    name: string;
    todayPressure: string;
    yesToday: string;
}

interface PreState {
    pressureObj: Pressure,
    addressTimePressureValueObj: Pressure
}

export const usePressureStore = defineStore('pressure', {
    state: (): { pressureObj: Pressure | null,
        addressTimePressureValueObj: Array<AddressState>,
        chartDataObj: Array<Pressure>
    } => ({
        // 初始数据
        pressureObj: null,
        // 地址时间查询数据
        addressTimePressureValueObj: [],
        // 图表数据
        chartDataObj: []
    }),
    actions: {
        // 获取表格数据
        addPressureObjActions(obj: Pressure) {
            this.pressureObj = obj
        },
        // 查询地点时间水压值 并展示表格
        async queryAddressPressureValue(address: string, time: string) {
            const res = await getPressureT(address, time)
            if (this.addressTimePressureValueObj) this.addressTimePressureValueObj = []
            if (this.chartDataObj) this.chartDataObj = []
            res.data.results.forEach((item: Pressure) => {
                // 图表数据
                this.chartDataObj.push(item)
                // 表格数据
                const {turnoverTime, pressure, oldPressure} = item
                this.addressTimePressureValueObj.push({
                    name: turnoverTime.slice(6),
                    todayPressure: pressure,
                    yesToday: oldPressure
                })
            })
        }
    },
    getters: {
        // 展示表格1
        screenPressure() {
            if (!this.pressureObj) return
            const data: any = reactive([
                {
                    name: '事件类型',
                    concreteValue: this.pressureObj!.pressure > '0.28' ? '限时高压' : '限时低压',
                },
                {
                    name: '开始时间',
                    concreteValue: this.pressureObj!.turnoverTime,
                },
                {
                    name: '事件来源',
                    concreteValue: this.pressureObj!.region,
                },
                {
                    name: '报警值',
                    concreteValue: this.pressureObj!.pressure + '  MPa',
                },
                {
                    name: '报警状态',
                    concreteValue: this.pressureObj!.pressure > '0.28' ? '报警中' : '未报警',
                },
                {
                    name: '恢复时间',
                    concreteValue: '--',
                },
                {
                    name: '持续时间',
                    concreteValue: this.pressureObj!.duration,
                },
            ])
            return data
        }
    }
})